package com.ys.peony.car300;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ccrv.cber.sort.ItemData;
import com.ys.peony.R;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BasicFragment;
import com.ys.peony.base.contract.CarPledgeContract;
import com.ys.peony.car300.ui.ChoiceCarTypeActivity;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.model.domain.CarAddInfo;
import com.ys.peony.model.domain.CarDetailModel;
import com.ys.peony.model.domain.CarInfo;
import com.ys.peony.model.domain.CarModel;
import com.ys.peony.model.domain.CarType;
import com.ys.peony.model.domain.CityModel;
import com.ys.peony.model.domain.SaveCarModel;
import com.ys.peony.presenter.CarPledgePresenter;
import com.ys.peony.ui.activity.CarPledgeActivity;
import com.ys.peony.ui.widget.DatePicker;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.ToastUtils;
import com.ys.peony.utils.YsUtil;
import com.ys.peony.view.YsDialogItemModel;
import com.ys.peony.view.inter.YsDailogClickListener;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 59153 on 2017/8/7.
 */

public class PledgeFragment extends BasicFragment<CarPledgeContract.ICarUpdateView, CarPledgePresenter> implements View.OnClickListener, DatePicker.DatePickerCallBack, YsDailogClickListener, CarPledgeContract.ICarUpdateView {

    @BindView(R.id.rootview)
    ScrollView rootView;

    @BindView(R.id.car_num)
    RelativeLayout car_num;
    @BindView(R.id.car_logo)
    RelativeLayout car_logo;
    @BindView(R.id.car_sort)
    RelativeLayout car_sort;
    @BindView(R.id.car_color)
    RelativeLayout car_color;
    @BindView(R.id.car_gongkuang)
    RelativeLayout gongkuang;
    @BindView(R.id.car_decorate)
    RelativeLayout car_decorate;
    @BindView(R.id.car_apperance)
    RelativeLayout car_apperance;


    @BindView(R.id.car_num_tv)
    TextView car_num_tv;
    @BindView(R.id.car_logo_tv)
    TextView car_logo_tv;
    @BindView(R.id.car_sort_tv)
    TextView car_sort_tv;
    @BindView(R.id.car_color_tv)
    TextView car_color_tv;
    @BindView(R.id.car_gongkuang_tv)
    TextView car_gongkuang_tv;
    @BindView(R.id.car_decorate_tv)
    TextView car_decorate_tv;
    @BindView(R.id.car_apperance_tv)
    TextView car_apperance_tv;

    @BindView(R.id.car_city)
    RelativeLayout car_city;
    @BindView(R.id.car_area)
    RelativeLayout car_area;
    @BindView(R.id.signin_time)
    RelativeLayout signin_time;
    @BindView(R.id.car_qs)
    RelativeLayout car_qs;
    @BindView(R.id.fazheng_time)
    RelativeLayout fazheng_time;
    @BindView(R.id.chuchang_time)
    RelativeLayout chuchang_time;

    @BindView(R.id.car_city_tv)
    TextView car_city_tv;
    @BindView(R.id.car_area_tv)
    TextView car_area_tv;
    @BindView(R.id.signin_time_tv)
    TextView signin_time_tv;
    @BindView(R.id.car_qs_tv)
    TextView car_qs_tv;
    @BindView(R.id.fazheng_time_tv)
    TextView fazheng_time_tv;
    @BindView(R.id.chuchang_time_tv)
    TextView chuchang_time_tv;


    @BindView(R.id.vin_editor)
    EditText vin_editor;
    @BindView(R.id.guohu_et)
    EditText guohu_et;
    @BindView(R.id.kilo_et)
    EditText kilo_et;
    @BindView(R.id.car_number_et)
    EditText car_number_et;
    @BindView(R.id.car_buy_et)
    EditText car_buy_et;
    @BindView(R.id.drivernum_et)
    EditText drivernum_et;
    @BindView(R.id.qs_et)
    EditText qs_et;
    @BindView(R.id.save)
    Button save;


    private final static int REQUEST_CAR = 1;
    private final static int REQUEST_CITY = 2;
    private CarModel.ModelInfoBean mCarInfo = null;
    private DatePicker datePicker = null;
    private YsDialogItemModel mDialogItemModel;
    private String selectYearStr = "", selectMonthStr = "", selectDayStr = "";

    private int mPosition = YSConstant.INVALID; // 标记当前车辆index;

    private int mOperateType = YSConstant.INVALID;

    public void setDataIndex(int position) {
        mPosition = position;
    }

    @Override
    protected CarPledgePresenter initPresenter() {
        return new CarPledgePresenter();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_pledge_info;
    }

    @Override
    public void initView() {
        initData();
        if (mOperateType == CarPledgeActivity.TYPE_OPERATE_UPDATE) {
            initListener();
            iniWidget();
        } else if (mOperateType == CarPledgeActivity.TYPE_OPERATE_SEE) {
            vin_editor.setFocusableInTouchMode(false);
            guohu_et.setFocusableInTouchMode(false);
            kilo_et.setFocusableInTouchMode(false);
            car_number_et.setFocusableInTouchMode(false);
            car_buy_et.setFocusableInTouchMode(false);
            drivernum_et.setFocusableInTouchMode(false);
            qs_et.setFocusableInTouchMode(false);
            save.setVisibility(View.GONE);
        }
    }

    private void iniWidget() {
        mDialogItemModel = new YsDialogItemModel(getContext(), this);

        vin_editor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                vin_editor.removeTextChangedListener(this);//解除文字改变事件
                car_logo_tv.setText("");
                car_sort_tv.setText("");
                car_num_tv.setText("");
                vin_editor.setText(charSequence.toString().toUpperCase());
                vin_editor.setSelection(charSequence.toString().length());//重新设置光标位置
                vin_editor.addTextChangedListener(this);//重新绑
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        vin_editor.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String input = vin_editor.getText().toString().trim();
                    if (presenter.isValidInput(input)) {
                        hideInput();
                        LoadingProgressUtils.showProgressDialog(getContext());
                        presenter.getCarByVin(input);
                    }
                }
                return false;
            }
        });

        car_number_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                car_number_et.removeTextChangedListener(this);//解除文字改变事件
                car_number_et.setText(charSequence.toString().toUpperCase());
                car_number_et.setSelection(charSequence.toString().length());//重新设置光标位置
                car_number_et.addTextChangedListener(this);//重新绑
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        drivernum_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                drivernum_et.removeTextChangedListener(this);//解除文字改变事件
                drivernum_et.setText(charSequence.toString().toUpperCase());
                drivernum_et.setSelection(charSequence.toString().length());//重新设置光标位置
                drivernum_et.addTextChangedListener(this);//重新绑
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }


    private void initData() {
        CarDetailModel carDetailModel = CarDataManager.getInstance().getCarDetailModel();
        if (carDetailModel == null) {
            return;
        }
        CarDetailModel.BusiMortgageVoBean busiMortgageVoBean = CarDataManager.getInstance().getCarDetailModel().getBusiMortgageVo();
        List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> list = busiMortgageVoBean.getBusivehicleandslipvoList();
        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBean = list.get(mPosition).get(0);
        vin_editor.setText(busivehicleandslipvoListBean.getVehicle_id().toString().trim());
        car_num_tv.setText(busivehicleandslipvoListBean.getModel_id_desr().toString().trim());
        car_logo_tv.setText(busivehicleandslipvoListBean.getBrand_name().toString().trim());
        car_sort_tv.setText(busivehicleandslipvoListBean.getSeries_id_desr().toString().trim());


        String colorId = busivehicleandslipvoListBean.getColor().toString().trim();
        String colorText = presenter.getItemTextById(carDetailModel.getVehicleCarcolorList(), R.id.car_color, colorId);
        if (!TextUtils.isEmpty(colorText)) {
            car_color_tv.setText(colorText);
        }

        String gongkuanId = busivehicleandslipvoListBean.getWork_state().toString().trim();
        String gongkuanText = presenter.getItemTextById(carDetailModel.getVehicleCarworkstateList(), R.id.car_gongkuang, gongkuanId);
        if (!TextUtils.isEmpty(gongkuanText)) {
            car_gongkuang_tv.setText(gongkuanText);
        }

        String decorateId = busivehicleandslipvoListBean.getInterior().toString().trim();
        String decorateText = presenter.getItemTextById(carDetailModel.getVehicleCarinteriorList(), R.id.car_decorate, decorateId);
        if (!TextUtils.isEmpty(decorateText)) {
            car_decorate_tv.setText(decorateText);
        }
        String apperanceId = busivehicleandslipvoListBean.getSurface().toString().trim();
        String apperanceText = presenter.getItemTextById(carDetailModel.getVehicleCarsurfaceList(), R.id.car_apperance, apperanceId);

        if (!TextUtils.isEmpty(apperanceText)) {
            car_apperance_tv.setText(apperanceText);
        }

        String areaId = String.valueOf(busivehicleandslipvoListBean.getAttribution_area());
        String areaText = presenter.getItemTextById(CarDataManager.getInstance().getCarDetailModel().getAreaList(), R.id.car_area, areaId);
        if (!TextUtils.isEmpty(areaText)) {
            car_area_tv.setText(areaText);
        }


        guohu_et.setText(String.valueOf(busivehicleandslipvoListBean.getTransfer_times()));
        car_city_tv.setText(busivehicleandslipvoListBean.getCity_id_desr().toString().trim());
        kilo_et.setText(String.valueOf(busivehicleandslipvoListBean.getKilometer()));
        String plate_number = busivehicleandslipvoListBean.getPlate_number().toString().trim();
        car_number_et.setText(plate_number.substring(2));
        car_buy_et.setText(String.valueOf(busivehicleandslipvoListBean.getPrice()));
        drivernum_et.setText(busivehicleandslipvoListBean.getEngine_number().toString().trim());
        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.TimeBuyingBean timeBuyingBean = busivehicleandslipvoListBean.getTime_buying();
        if (timeBuyingBean != null) {
            signin_time_tv.setText(YsUtil.getDateToString(Long.valueOf(timeBuyingBean.getTime())));//购买时间==注册时间
        }

        String ownership_tag = busivehicleandslipvoListBean.getOwnership_tag();
        String ownership_text = presenter.getItemTextById(CarDataManager.getInstance().getCarDetailModel().getVehicleOwnershopTagList(), R.id.car_qs, ownership_tag);
        if (!TextUtils.isEmpty(ownership_text)) {
            car_qs_tv.setText(ownership_text);//权属类型
        }

        if ("2".equals(ownership_tag)) {
            qs_et.setFocusableInTouchMode(false);
            qs_et.setFocusable(false);
        } else if ("1".equals(ownership_tag)) {
            qs_et.setFocusableInTouchMode(true);
            qs_et.setFocusable(true);
        }
        qs_et.setText(busivehicleandslipvoListBean.getOwnership_person().toString().trim());
        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.TimeAwardedBean timeAwardedBean = busivehicleandslipvoListBean.getTime_awarded();
        if (timeAwardedBean != null) {
            fazheng_time_tv.setText(YsUtil.getDateToString(Long.valueOf(timeAwardedBean.getTime())));
        }
        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.MakeDateBean makeDateBean = busivehicleandslipvoListBean.getMake_date();
        if (makeDateBean != null) {
            chuchang_time_tv.setText(YsUtil.getDateToString(Long.valueOf(makeDateBean.getTime())));
        }


        //存储页面当前未修改的提交信息
        CarAddInfo carAddInfo = CarDataManager.getInstance().getmCarAddInfo();
        carAddInfo.setModel_id(String.valueOf(busivehicleandslipvoListBean.getModel_id()));
        carAddInfo.setModel_id_desr(busivehicleandslipvoListBean.getModel_id_desr());
        carAddInfo.setVehicle_type(busivehicleandslipvoListBean.getVehicle_type());
        carAddInfo.setBrand_name(car_logo_tv.getText().toString().trim());
        carAddInfo.setBrand_id(String.valueOf(busivehicleandslipvoListBean.getBrand_id()));
        carAddInfo.setSeries_id(String.valueOf(busivehicleandslipvoListBean.getSeries_id()));
        carAddInfo.setSeries_id_desr(busivehicleandslipvoListBean.getSeries_id_desr());
        carAddInfo.setVehicle_id(busivehicleandslipvoListBean.getVehicle_id());
        carAddInfo.setTransfer_times(guohu_et.getText().toString().trim());
        carAddInfo.setLoanID(CarDataManager.getInstance().getLoanId());
        carAddInfo.setId(String.valueOf(busivehicleandslipvoListBean.getVehicle_code()));
        carAddInfo.setWork_state(busivehicleandslipvoListBean.getWork_state());
        carAddInfo.setColor(busivehicleandslipvoListBean.getColor());
        carAddInfo.setInterior(busivehicleandslipvoListBean.getInterior());
        carAddInfo.setSurface(busivehicleandslipvoListBean.getSurface());
        carAddInfo.setAttribution_area(String.valueOf(busivehicleandslipvoListBean.getAttribution_area()));
        carAddInfo.setOwnership_tag(busivehicleandslipvoListBean.getOwnership_tag());
        carAddInfo.setCity_id(String.valueOf(busivehicleandslipvoListBean.getCity_id()));
        carAddInfo.setCity_id_desr(busivehicleandslipvoListBean.getCity_id_desr());
        carAddInfo.setProv_id(String.valueOf(busivehicleandslipvoListBean.getProv_id()));
        carAddInfo.setProv_id_desr(busivehicleandslipvoListBean.getProv_id_desr());
    }

    private void initListener() {
        car_sort.setOnClickListener(this);
        car_logo.setOnClickListener(this);
        car_color.setOnClickListener(this);
        car_decorate.setOnClickListener(this);
        car_apperance.setOnClickListener(this);
        gongkuang.setOnClickListener(this);
        car_num.setOnClickListener(this);

        car_city.setOnClickListener(this);
        car_area.setOnClickListener(this);
        signin_time.setOnClickListener(this);
        car_qs.setOnClickListener(this);
        fazheng_time.setOnClickListener(this);
        chuchang_time.setOnClickListener(this);
        save.setOnClickListener(this);
        vin_editor.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        List<ItemModel> itemModels;
        CarDetailModel carDetailModel = CarDataManager.getInstance().getCarDetailModel();
        if (carDetailModel == null) {
            return;
        }
        datePicker = null;
        switch (view.getId()) {
            case R.id.car_num:
                if (TextUtils.isEmpty(car_sort_tv.getText().toString().trim()) || TextUtils.isEmpty(car_logo_tv.getText().toString().trim())) {
                    ToastUtils.showToast("请先输入vin码");
                    return;
                }
                if (presenter.isValidInput(vin_editor.getText().toString().trim())) {
                    CarModel carModel = CarDataManager.getInstance().getCarVinModel();
                    if (carModel == null) {
                        LoadingProgressUtils.showProgressDialog(getActivity());
                        presenter.getCarByVin(vin_editor.getText().toString().trim());
                    } else {
                        jumpCarTypeSelect();
                    }
                }

                break;

            case R.id.car_gongkuang:
                itemModels = presenter.getItemModel(carDetailModel.getVehicleCarworkstateList(), view.getId());
                if (itemModels != null && itemModels.size() > 0) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_GONGKUANG);
                }
                break;
            case R.id.car_color:
                itemModels = presenter.getItemModel(carDetailModel.getVehicleCarcolorList(), view.getId());
                if (itemModels != null && itemModels.size() > 0) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_COLOR);
                }
                break;
            case R.id.car_decorate:
                itemModels = presenter.getItemModel(carDetailModel.getVehicleCarinteriorList(), view.getId());
                if (itemModels != null && itemModels.size() > 0) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_DECORATE);
                }
                break;
            case R.id.car_apperance:
                itemModels = presenter.getItemModel(carDetailModel.getVehicleCarsurfaceList(), view.getId());
                if (itemModels != null && itemModels.size() > 0) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_APPERANCE);
                }
                break;
            case R.id.car_city:
                if (CarDataManager.getInstance().getmCityModel() == null) {
                    LoadingProgressUtils.showProgressDialog(getContext());
                    presenter.getCity();
                } else {
                    jumpCitySelect();
                }
                break;
            case R.id.car_area:
                itemModels = presenter.getItemModel(CarDataManager.getInstance().getCarDetailModel().getAreaList(), R.id.car_area);
                if (itemModels != null && itemModels.size() > 0) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_AREA);
                }
                break;
            case R.id.signin_time:
                if (datePicker == null) {
                    datePicker = new DatePicker(getContext(), this, YSConstant.POP_DATE_SIGNTIME);
                }
                datePicker.show(rootView, selectYearStr, selectMonthStr, selectDayStr);
                break;
            case R.id.car_qs:
                itemModels = presenter.getItemModel(CarDataManager.getInstance().getCarDetailModel().getVehicleOwnershopTagList(), R.id.car_qs);
                if (itemModels != null && itemModels.size() > 0) {
                    mDialogItemModel.setItemModels(itemModels, YSConstant.POP_TYPE_QS);
                }
                break;
            case R.id.fazheng_time:
                if (datePicker == null) {
                    datePicker = new DatePicker(getContext(), this, YSConstant.POP_DATE_FAZHENGTIME);
                }
                datePicker.show(rootView, selectYearStr, selectMonthStr, selectDayStr);
                break;
            case R.id.chuchang_time:
                if (datePicker == null) {
                    datePicker = new DatePicker(getContext(), this, YSConstant.POP_DATE_CHUCHANGTIME);
                }
                datePicker.show(rootView, selectYearStr, selectMonthStr, selectDayStr);
                break;
            case R.id.save:
                if (checkCarInfo()) {
                    LoadingProgressUtils.showProgressDialog(getContext());
                    saveCarInfo();
                    presenter.updatePledgeCar();
                } else {
                    ToastUtils.showToast("请完善必要信息");
                }
                break;
            case R.id.car_logo:
                ToastUtils.showToast("车辆品牌不可更改");
                break;
            case R.id.car_sort:
                ToastUtils.showToast("车系不可更改");
                break;
            case R.id.vin_editor:
                vin_editor.setCursorVisible(true);
                break;
        }
    }

    private void jumpCarTypeSelect() {
        if (CarDataManager.getInstance().getCarVinModel().getModelInfo().size() > 1) {
            skipToTargetPage(ChoiceCarTypeActivity.class, null, SwipeDirect.DIRECT_LEFT, true, REQUEST_CAR);
        } else {
            ToastUtils.showToast("无其他车型选择");
        }
    }

    private boolean checkCarInfo() {
        if (TextUtils.isEmpty(vin_editor.getText().toString().trim()) ||
                TextUtils.isEmpty(car_num_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(car_logo_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(car_sort_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(car_color_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(car_gongkuang_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(car_decorate_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(car_apperance_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(guohu_et.getText().toString().trim()) ||
                TextUtils.isEmpty(car_city_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(fazheng_time_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(kilo_et.getText().toString().trim()) ||
                TextUtils.isEmpty(car_number_et.getText().toString().trim()) ||
                TextUtils.isEmpty(car_buy_et.getText().toString().trim()) ||
                TextUtils.isEmpty(drivernum_et.getText().toString().trim()) ||
                TextUtils.isEmpty(signin_time_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(car_qs_tv.getText().toString().trim()) ||
                TextUtils.isEmpty(qs_et.getText().toString().trim()) ||
                TextUtils.isEmpty(car_area_tv.getText().toString().trim())) {
            return false;
        }
        return true;
    }

    private void saveCarInfo() {
        CarAddInfo carAddInfo = CarDataManager.getInstance().getmCarAddInfo();
        if (mCarInfo != null) {
            carAddInfo.setVehicle_type(car_num_tv.getText().toString().trim());
            carAddInfo.setBrand_name(car_logo_tv.getText().toString().trim());
            carAddInfo.setBrand_id(String.valueOf(mCarInfo.getBrand_id()));
            carAddInfo.setSeries_id(String.valueOf(mCarInfo.getSeries_id()));
            carAddInfo.setSeries_id_desr(mCarInfo.getSeries_name());
            carAddInfo.setTransfer_times(guohu_et.getText().toString().trim());
            carAddInfo.setLoanID(CarDataManager.getInstance().getLoanId());
        }
        carAddInfo.setKilometer(kilo_et.getText().toString().trim());
        carAddInfo.setPlate_number(car_number_et.getText().toString().trim());
        carAddInfo.setPrice(car_buy_et.getText().toString().trim());
        carAddInfo.setEngine_number(drivernum_et.getText().toString().trim());
        carAddInfo.setTime_buying(signin_time_tv.getText().toString().trim());
        carAddInfo.setOwnership_person(qs_et.getText().toString().trim());
        if (!TextUtils.isEmpty(fazheng_time_tv.getText().toString().trim())) {
            carAddInfo.setTime_awarded(fazheng_time_tv.getText().toString().trim());
        }
        if (!TextUtils.isEmpty(chuchang_time_tv.getText().toString().trim())) {
            carAddInfo.setMake_date(chuchang_time_tv.getText().toString().trim());
        }
        if (!TextUtils.isEmpty(SharePreferencesUtils.getInstance().getUserId())) {
            carAddInfo.setUserID(SharePreferencesUtils.getInstance().getUserId());
        }

    }

    @Override
    public void clickDialogItemModel(int type, ItemModel itemModel) {
        if (itemModel == null) {
            return;
        }
        switch (type) {
            case YSConstant.POP_TYPE_GONGKUANG:
                car_gongkuang_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmCarAddInfo().setWork_state(itemModel.getVal());
                break;
            case YSConstant.POP_TYPE_COLOR:
                car_color_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmCarAddInfo().setColor(itemModel.getVal());
                break;
            case YSConstant.POP_TYPE_DECORATE:
                car_decorate_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmCarAddInfo().setInterior(itemModel.getVal());
                break;
            case YSConstant.POP_TYPE_APPERANCE:
                car_apperance_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmCarAddInfo().setSurface(itemModel.getVal());
                break;

            /////////////////////////////////////
            case YSConstant.POP_TYPE_AREA:
                car_area_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmCarAddInfo().setAttribution_area(itemModel.getVal());
                break;
            case YSConstant.POP_TYPE_QS:
                if ("2".equals(itemModel.getVal())) {
                    qs_et.setFocusableInTouchMode(false);
                    qs_et.setFocusable(false);
                } else if ("1".equals(itemModel.getVal())) {
                    qs_et.setFocusableInTouchMode(true);
                    qs_et.setFocusable(true);
                }
                car_qs_tv.setText(itemModel.getText());
                CarDataManager.getInstance().getmCarAddInfo().setOwnership_tag(itemModel.getVal());
                break;

        }
    }

    @Override
    public void datePickerConfirmOnclick(String yearStr, String monthStr, String dayStr, int tag) {
        selectYearStr = yearStr;
        selectMonthStr = monthStr;
        selectDayStr = dayStr;
        String time = yearStr + "-" + monthStr + "-" + dayStr;
        switch (tag) {
            case YSConstant.POP_DATE_SIGNTIME:
                signin_time_tv.setText(time);
                break;
            case YSConstant.POP_DATE_FAZHENGTIME:
                fazheng_time_tv.setText(time);
                break;
            case YSConstant.POP_DATE_CHUCHANGTIME:
                chuchang_time_tv.setText(time);
                break;

        }
    }

    @Override
    public void datePickerCancelOnclick() {

    }

    private void jumpCitySelect() {
        String[] items = getCityItems();
        if (items != null && items.length > 0) {
            Bundle bundle = new Bundle();
            bundle.putStringArray(ListSelectActivity.ITEM_PARAM, items);
            skipToTargetPage(ListSelectActivity.class, bundle, SwipeDirect.DIRECT_LEFT, true, REQUEST_CITY);
        }
    }

    private String[] getCityItems() {
        List<String> items = new ArrayList<>();
        CityModel cityModel = CarDataManager.getInstance().getmCityModel();
        if (cityModel != null && cityModel.getData() != null) {
            List<CityModel.DataBean> cityModelData = cityModel.getData();
            for (int i = 0; i < cityModelData.size(); i++) {
                List<CityModel.DataBean.CityInfosBean> cityInfosBeen = cityModelData.get(i).getCityInfos();
                for (int j = 0; j < cityInfosBeen.size(); j++) {
                    CityModel.DataBean.CityInfosBean cityInfoBean = cityInfosBeen.get(j);
                    if (cityInfoBean != null && !TextUtils.isEmpty(cityInfoBean.getCity_name())) {
                        items.add(cityInfoBean.getCity_name());
                    }
                }
            }
        }
        String[] result = new String[items.size()];
        items.toArray(result);
        return result;
    }

    private void updateView(CarModel carModel) {
        if (carModel != null) {
            List<CarModel.ModelInfoBean> modelInfoBeanList = carModel.getModelInfo();
            if (modelInfoBeanList == null || modelInfoBeanList.size() == 0) {
                return;
            }
            CarDataManager.getInstance().getmCarAddInfo().setVehicle_id(vin_editor.getText().toString().trim());
            if (modelInfoBeanList.size() == 1) {
                CarModel.ModelInfoBean modelInfoBean = modelInfoBeanList.get(0);
                mCarInfo = modelInfoBean;
                saveCarParam();
                CarAddInfo carAddInfo = CarDataManager.getInstance().getmCarAddInfo();
                carAddInfo.setModel_id(String.valueOf(mCarInfo.getModel_id()));
                carAddInfo.setModel_id_desr(mCarInfo.getModel_name());
                car_num_tv.setText(modelInfoBean.getModel_name());
                car_logo_tv.setText(modelInfoBean.getBrand_name());
                car_sort_tv.setText(modelInfoBean.getSeries_name());
                ToastUtils.showToast("无其他车型选择");
            } else {
                jumpCarTypeSelect();
            }
        }
    }

    @Override
    public void onFail(String msg) {
        LoadingProgressUtils.closeDialog();
        ToastUtils.showToast(msg);
    }

    @Override
    public void onSuccess(int code) {
        switch (code) {
            case CarPledgeContract.GET_CAR_VIN:
                updateView(CarDataManager.getInstance().getCarVinModel());
                break;
            case CarPledgeContract.SAVE_CAR_INFO:
                SaveCarModel saveCarModel = CarDataManager.getInstance().getSaveCarModel();
                if (saveCarModel != null) {
                    SaveCarModel.MortgageListBean mortgageListBean = saveCarModel.getMortgageList().get(0);
                    if (mortgageListBean.getEstimation() == 0) {
                        ToastUtils.showToast(this.getResources().getString(R.string.car_error_tips));
                    }
                }
                presenter.getCarInfoById(CarDataManager.getInstance().getLoanId());
                break;
            case CarPledgeContract.GET_CAR_DETAIL:
                LoadingProgressUtils.closeDialog();
                ToastUtils.showToast("修改成功");
                //回退跳到主界面
                skipToTargetPage(CarPledgeActivity.class, null);
                break;
            case CarPledgeContract.CODE_GET_CITY:
                jumpCitySelect();
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ListSelectActivity.RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CAR:
                    int position = data.getIntExtra(ChoiceCarTypeActivity.RESULT_POSITION, YSConstant.INVALID);
                    CarModel carModel = CarDataManager.getInstance().getCarVinModel();
                    if (carModel == null || position == YSConstant.INVALID || position > carModel.getModelInfo().size()) {
                        return;
                    }

                    CarModel.ModelInfoBean modelInfoBean = carModel.getModelInfo().get(position);
                    if (modelInfoBean != null && !TextUtils.isEmpty(modelInfoBean.getModel_name())) {
                        mCarInfo = modelInfoBean;
                        saveCarParam();
                        car_num_tv.setText(modelInfoBean.getModel_name());
                        car_logo_tv.setText(modelInfoBean.getBrand_name());
                        car_sort_tv.setText(modelInfoBean.getSeries_name());
                    }
                    CarAddInfo carAddInfo = CarDataManager.getInstance().getmCarAddInfo();
                    carAddInfo.setModel_id(String.valueOf(modelInfoBean.getModel_id()));
                    carAddInfo.setModel_id_desr(modelInfoBean.getModel_name());
                    break;
                case REQUEST_CITY:
                    ItemData cityType = (ItemData) data.getSerializableExtra(ListSelectActivity.ITEM_PARAM);
                    if (cityType != null && !TextUtils.isEmpty(cityType.getCar_title())) {
                        car_city_tv.setText(cityType.getCar_title());
                        CityModel.DataBean.CityInfosBean cityInfosBean = getSelectCityInfoByName(cityType.getCar_title());
                        if (cityInfosBean != null) {
                            CarAddInfo carAddInfo2 = CarDataManager.getInstance().getmCarAddInfo();
                            carAddInfo2.setProv_id(String.valueOf(cityInfosBean.getProv_id()));
                            if (cityInfosBean.getProv_name() != null) {
                                carAddInfo2.setProv_id_desr(cityInfosBean.getProv_name().toString());
                            }
                            carAddInfo2.setCity_id(String.valueOf(cityInfosBean.getCity_id()));
                            carAddInfo2.setCity_id_desr(cityInfosBean.getCity_name());
                        }
                    }
                    break;
            }
        }
    }

    private CityModel.DataBean.CityInfosBean getSelectCityInfoByName(String cityName) {
        CityModel cityModel = CarDataManager.getInstance().getmCityModel();
        if (cityModel != null && cityModel.getData() != null) {
            List<CityModel.DataBean> cityModelData = cityModel.getData();
            for (int i = 0; i < cityModelData.size(); i++) {
                List<CityModel.DataBean.CityInfosBean> cityInfosBeen = cityModelData.get(i).getCityInfos();
                for (int j = 0; j < cityInfosBeen.size(); j++) {
                    CityModel.DataBean.CityInfosBean cityInfoBean = cityInfosBeen.get(j);
                    if (cityInfoBean != null && cityName.equals(cityInfoBean.getCity_name())) {
                        return cityInfoBean;
                    }
                }
            }
        }
        return null;
    }

    private void saveCarParam() {
        if (mCarInfo != null && mCarInfo.getDiff_params() != null) {
            CarModel.ModelInfoBean.DiffParamsBean.StandardGroupBean standardGroupBean = mCarInfo.getDiff_params().getStandard_group();
            if (standardGroupBean != null) {
                try {
                    Field[] fields = standardGroupBean.getClass().getDeclaredFields();
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < fields.length - 2; i++) {
                        String value = String.valueOf(fields[i].get(standardGroupBean));
                        if (ChoiceCarTypeActivity.CONFIG_TYPE.equals(value)) {
                            list.add(fields[i].getName());
                        }
                    }
                    if (list.size() > 0) {
                        CarAddInfo.CarTypeParam carTypeParam = new CarAddInfo.CarTypeParam();
                        carTypeParam.setOne(list);
                        CarDataManager.getInstance().getmCarAddInfo().setCarTypeParam(carTypeParam);
                    } else {
                        CarDataManager.getInstance().getmCarAddInfo().setCarTypeParam(null);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void setType(int type) {
        mOperateType = type;
    }
}
